package com.home.oss.controller;

import com.home.base.core.framework.msg.ServerResponse;
import com.home.oss.entity.OssBizFile;
import com.home.oss.factory.FileFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * Description:
 * Created by 王大宸 on 2020-10-15 21:37
 * Created with IntelliJ IDEA.
 */
@RestController
@RequestMapping(value = "/oss")
public class OssController {
    @Autowired
    private FileFactory fileFactory;

    /***
     * 功能说明：文件上传统一接口
     *
     * @author 王大宸
     * @date 2020/10/15 21:37
     * @param type   文件类型
     * @param file   文件  @RequestParam("file")
     * @return ServerResponse<BizFileMd5>
     */
    @PostMapping(value = "/upload/{type}")
    @ResponseBody
    public ServerResponse<OssBizFile> upload(@PathVariable("type") String type,
                                             MultipartFile file,
                                             HttpServletResponse response) {
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS,PUT, DELETE");
        response.setHeader("Access-Control-Allow-Credentials","true");
        return fileFactory.build().upload(file, type);
    }

}
